<?php
/*
 * Copyright (C) 2010 TopCoder Inc., All Rights Reserved.
 */
namespace protocol\translation\framework\services\base;

require_once('PHPUnit/Framework.php');
require_once('log4php/Logger.php');
require_once('protocol/translation/framework/services/Helper.php');
require_once('protocol/translation/framework/services/base/Converter.php');
require_once('protocol/translation/framework/services/base/SMPP_JSON_Converter.php');

use protocol\translation\framework\services\TestHelper;
use protocol\translation\framework\services\ConversionException;
use protocol\translation\framework\services\MissingMandatoryParametersException;

/**
 * <p>
 * Unit tests for the SMPP_JSON_Converter class.
 * </p>
 *
 * @author akinwale
 * @version 1.0
 */
class SMPP_JSON_ConverterTests extends \PHPUnit_Framework_TestCase {
    /**
     * <p>
     * The SMPP_JSON_Converter instance to be tested.
     * </p>
     *
     * @var SMPP_JSON_Converter
     */
    private $converter;

    /**
     * <p>
     * Setup for each unit test in the test case.
     * </p>
     */
    protected function setUp() {
        $this->converter = new SMPP_JSON_Converter();
    }

    /**
     * <p>
     * Cleanup after each unit test in the test case.
     * </p>
     */
    protected function tearDown() {
        $this->converter = null;
    }

    /**
     * <p>
     * Tests that the SMPP_JSON_Converter() constructor creates a new instance that
     * is not null.
     * </p>
     */
    public function testCtor() {
        $this->assertNotNull($this->converter, "converter should not be null");
    }

    /**
     * <p>
     * Tests that the convertTo(array, string) method works properly.
     * </p>
     */
    public function testConvertTo() {
        $json = $this->converter->convertTo(array(
            "address" => "addressValue",
            "message" => "The Message",
            "senderName" => "topcoder"
        ), "send sms");

        $expected  = "{\"address\":\"addressValue\",\"message\":\"The Message\",";
        $expected .= "\"senderName\":\"topcoder\"}";
        $this->assertEquals($expected, $json, "convertTo does not work properly");

        $json = $this->converter->convertTo(array("id" => "value"), "query delivery status");
        $expected = "{\"id\":\"value\"}";
        $this->assertEquals($expected, $json, "convertTo does not work properly");
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method works properly.
     * </p>
     */
    public function testConvertFrom() {
        $messageBody = TestHelper::buildMessageBody(
            array("notifyURL", "callbackData"), array("http://notify", "Callback"));
        $data = $this->converter->convertFrom($messageBody, "subscribeToSMSDeliveryNotifications");
        $error = "convertFrom does not work properly";
        $this->assertEquals("http://notify", $data["notifyURL"], $error);
        $this->assertEquals("Callback", $data["callbackData"], $error);

        $messageBody = TestHelper::buildMessageBody(array(
            "destinationAddress", "notifyURL", "criteria", "notificationFormat", "callbackData", "clientCorrelator"),
            array("dstAddr", "http://notify2", "none", "plaintext", "callback2", "correlate"));
        $data = $this->converter->convertFrom($messageBody, "subscribe to notifications");
        $this->assertEquals("dstAddr", $data["destinationAddress"], $error);
        $this->assertEquals("http://notify2", $data["notifyURL"], $error);
        $this->assertEquals("none", $data["criteria"], $error);
        $this->assertEquals("plaintext", $data["notificationFormat"], $error);
        $this->assertEquals("callback2", $data["callbackData"], $error);
        $this->assertEquals("correlate", $data["clientCorrelator"], $error);
    }

    /**
     * <p>
     * Tests that the convertTo(array, string) method handles a null value set for the
     * first parameter by throwing InvalidArgumentException.
     * </p>
     *
     * <p>
     * InvalidArgumentException is expected.
     * </p>
     */
    public function testConvertTo_Failure_FirstParamNull() {
        try {
            $this->converter->convertTo(null, "data");
            $this->fail("InvalidArgumentException was expected");
        } catch (\InvalidArgumentException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertTo(array, string) method handles a non-array value set
     * for the first parameter by throwing InvalidArgumentException.
     * </p>
     *
     * <p>
     * InvalidArgumentException is expected.
     * </p>
     */
    public function testConvertTo_Failure_FirstParamNotAnArray() {
        try {
            $this->converter->convertTo(1, "data");
            $this->fail("InvalidArgumentException was expected");
        } catch (\InvalidArgumentException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertTo(array, string) method handles a null value set for the
     * second parameter by throwing InvalidArgumentException.
     * </p>
     *
     * <p>
     * InvalidArgumentException is expected.
     * </p>
     */
    public function testConvertTo_Failure_SecondParamNull() {
        try {
            $this->converter->convertTo(array(), null);
            $this->fail("InvalidArgumentException was expected");
        } catch (\InvalidArgumentException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertTo(array, string) method handles an empty value set for the
     * second parameter by throwing InvalidArgumentException.
     * </p>
     *
     * <p>
     * InvalidArgumentException is expected.
     * </p>
     */
    public function testConvertTo_Failure_SecondParamEmptyString() {
        try {
            $this->converter->convertTo(array(), "");
            $this->fail("InvalidArgumentException was expected");
        } catch (\InvalidArgumentException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertTo(array, string) method handles a non-string value
     * set for the second parameter by throwing InvalidArgumentException.
     * </p>
     *
     * <p>
     * InvalidArgumentException is expected.
     * </p>
     */
    public function testConvertTo_Failure_SecondParamNotAString() {
        try {
            $this->converter->convertTo(array(), 1);
            $this->fail("InvalidArgumentException was expected");
        } catch (\InvalidArgumentException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a null value set for the
     * second parameter by throwing InvalidArgumentException.
     * </p>
     *
     * <p>
     * InvalidArgumentException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_FirstParamNull() {
        try {
            $this->converter->convertFrom(null, "str");
            $this->fail("InvalidArgumentException was expected");
        } catch (\InvalidArgumentException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles an empty value set for the
     * second parameter by throwing InvalidArgumentException.
     * </p>
     *
     * <p>
     * InvalidArgumentException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_FirstParamEmptyString() {
        try {
            $this->converter->convertFrom("", "str");
            $this->fail("InvalidArgumentException was expected");
        } catch (\InvalidArgumentException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a non-string value
     * set for the first parameter by throwing InvalidArgumentException.
     * </p>
     *
     * <p>
     * InvalidArgumentException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_FirstParamNotAString() {
        try {
            $this->converter->convertFrom(1, "str");
            $this->fail("InvalidArgumentException was expected");
        } catch (\InvalidArgumentException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a null value set for the
     * second parameter by throwing InvalidArgumentException.
     * </p>
     *
     * <p>
     * InvalidArgumentException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SecondParamNull() {
        try {
            $this->converter->convertFrom("str", null);
            $this->fail("InvalidArgumentException was expected");
        } catch (\InvalidArgumentException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles an empty value set for the
     * second parameter by throwing InvalidArgumentException.
     * </p>
     *
     * <p>
     * InvalidArgumentException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SecondParamEmptyString() {
        try {
            $this->converter->convertFrom("str", "");
            $this->fail("InvalidArgumentException was expected");
        } catch (\InvalidArgumentException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a non-string value
     * set for the second parameter by throwing InvalidArgumentException.
     * </p>
     *
     * <p>
     * InvalidArgumentException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SecondParamNotAString() {
        try {
            $this->converter->convertFrom("str", 1);
            $this->fail("InvalidArgumentException was expected");
        } catch (\InvalidArgumentException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(array, string) method handles an operation that
     * does not exist by throwing throwing InvalidArgumentException.
     * </p>
     *
     * <p>
     * InvalidArgumentException is expected.
     * </p>
     */
    public function testConvertTo_Failure_InvalidOperationName() {
        try {
            $this->converter->convertFrom(array(), "invalidOps");
            $this->fail("InvalidArgumentException was expected");
        } catch (\InvalidArgumentException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles an operation that
     * does not exist by throwing throwing InvalidArgumentException.
     * </p>
     *
     * <p>
     * InvalidArgumentException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_InvalidOperationName() {
        try {
            $this->converter->convertFrom("str", "invalidOps");
            $this->fail("InvalidArgumentException was expected");
        } catch (\InvalidArgumentException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a case where
     * a mandatory parameter is missing for the "send sms" operation by throwing
     * MissingMandatoryParametersException.
     * </p>
     *
     * <p>
     * MissingMandatoryParametersException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SendSms_MissingMandatoryParam1() {
        $messageBody = TestHelper::buildMessageBody(
            array("senderAddress", "message", "clientCorrelator", "notifyURL", "senderName"),
            array("sender", "Msg", "correlator", "http://notify", "topcoder"));
        try {
            $this->converter->convertFrom($messageBody, "send sms");
            fail("MissingMandatoryParametersException was expected");
        } catch (MissingMandatoryParametersException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a case where
     * a mandatory parameter is missing for the "send sms" operation by throwing
     * MissingMandatoryParametersException.
     * </p>
     *
     * <p>
     * MissingMandatoryParametersException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SendSms_MissingMandatoryParam2() {
        $messageBody = TestHelper::buildMessageBody(
            array("address", "message", "clientCorrelator", "notifyURL", "senderName"),
            array("addr", "Msg", "correlator", "http://notify", "topcoder"));
        try {
            $this->converter->convertFrom($messageBody, "send sms");
            fail("MissingMandatoryParametersException was expected");
        } catch (MissingMandatoryParametersException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a case where
     * a mandatory parameter is missing for the "send sms" operation by throwing
     * MissingMandatoryParametersException.
     * </p>
     *
     * <p>
     * MissingMandatoryParametersException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SendSms_MissingMandatoryParam3() {
        $messageBody = TestHelper::buildMessageBody(
            array("senderAddress", "address", "clientCorrelator", "notifyURL", "senderName"),
            array("sender", "Addr", "correlator", "http://notify", "topcoder"));
        try {
            $this->converter->convertFrom($messageBody, "send sms");
            fail("MissingMandatoryParametersException was expected");
        } catch (MissingMandatoryParametersException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a case where
     * a mandatory parameter is missing for the "send sms" operation by throwing
     * MissingMandatoryParametersException.
     * </p>
     *
     * <p>
     * MissingMandatoryParametersException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SendSms_MissingMandatoryParam4() {
        $messageBody = TestHelper::buildMessageBody(
            array("senderAddress", "address", "message", "notifyURL", "senderName"),
            array("sender", "Addr", "Msg", "http://notify", "topcoder"));
        try {
            $this->converter->convertFrom($messageBody, "send sms");
            fail("MissingMandatoryParametersException was expected");
        } catch (MissingMandatoryParametersException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a case where
     * a mandatory parameter is missing for the "send sms" operation by throwing
     * MissingMandatoryParametersException.
     * </p>
     *
     * <p>
     * MissingMandatoryParametersException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SendSms_MissingMandatoryParam5() {
        $messageBody = TestHelper::buildMessageBody(
            array("senderAddress", "address", "message", "clientCorrelator", "senderName"),
            array("sender", "Addr", "Msg", "correlator", "topcoder"));
        try {
            $this->converter->convertFrom($messageBody, "send sms");
            fail("MissingMandatoryParametersException was expected");
        } catch (MissingMandatoryParametersException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a case where
     * a mandatory parameter is missing for the "send sms" operation by throwing
     * MissingMandatoryParametersException.
     * </p>
     *
     * <p>
     * MissingMandatoryParametersException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SendSms_MissingMandatoryParam6() {
        $messageBody = TestHelper::buildMessageBody(
            array("senderAddress", "address", "message", "clientCorrelator", "notifyURL"),
            array("sender", "Addr", "Msg", "correlator", "http://notify"));
        try {
            $this->converter->convertFrom($messageBody, "send sms");
            fail("MissingMandatoryParametersException was expected");
        } catch (MissingMandatoryParametersException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a case where
     * a mandatory parameter is missing for the "subscribeToSMSDeliveryNotifications"
     * operation by throwing MissingMandatoryParametersException.
     * </p>
     *
     * <p>
     * MissingMandatoryParametersException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SubscribeToSMSDeliveryNotifications_MissingMandatoryParam1() {
        $messageBody = TestHelper::buildMessageBody(array("callbackData"), array("Callback"));
        try {
            $this->converter->convertFrom($messageBody, "subscribeToSMSDeliveryNotifications");
            fail("MissingMandatoryParametersException was expected");
        } catch (MissingMandatoryParametersException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a case where
     * a mandatory parameter is missing for the "subscribeToSMSDeliveryNotifications"
     * operation by throwing MissingMandatoryParametersException.
     * </p>
     *
     * <p>
     * MissingMandatoryParametersException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SubscribeToSMSDeliveryNotifications_MissingMandatoryParam2() {
        $messageBody = TestHelper::buildMessageBody(array("notifyURL"), array("http://notify"));
        try {
            $this->converter->convertFrom($messageBody, "subscribeToSMSDeliveryNotifications");
            fail("MissingMandatoryParametersException was expected");
        } catch (MissingMandatoryParametersException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a case where
     * a mandatory parameter is missing for the "subscribe to notifications"
     * operation by throwing MissingMandatoryParametersException.
     * </p>
     *
     * <p>
     * MissingMandatoryParametersException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SubscribeToNotifications_MissingMandatoryParam1() {
        $messageBody = TestHelper::buildMessageBody(array(
            "notifyURL", "criteria", "notificationFormat", "callbackData", "clientCorrelator"),
            array("http://notify2", "none", "plaintext", "callback2", "correlate"));
        try {
            $this->converter->convertFrom($messageBody, "subscribe to notifications");
            fail("MissingMandatoryParametersException was expected");
        } catch (MissingMandatoryParametersException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a case where
     * a mandatory parameter is missing for the "subscribe to notifications"
     * operation by throwing MissingMandatoryParametersException.
     * </p>
     *
     * <p>
     * MissingMandatoryParametersException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SubscribeToNotifications_MissingMandatoryParam2() {
        $messageBody = TestHelper::buildMessageBody(array(
            "destinationAddress", "criteria", "notificationFormat", "callbackData", "clientCorrelator"),
            array("dstAddr", "none", "plaintext", "callback2", "correlate"));
        try {
            $this->converter->convertFrom($messageBody, "subscribe to notifications");
            fail("MissingMandatoryParametersException was expected");
        } catch (MissingMandatoryParametersException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a case where
     * a mandatory parameter is missing for the "subscribe to notifications"
     * operation by throwing MissingMandatoryParametersException.
     * </p>
     *
     * <p>
     * MissingMandatoryParametersException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SubscribeToNotifications_MissingMandatoryParam3() {
        $messageBody = TestHelper::buildMessageBody(array(
            "destinationAddress", "notifyURL", "notificationFormat", "callbackData", "clientCorrelator"),
            array("dstAddr", "http://notify2", "plaintext", "callback2", "correlate"));
        try {
            $this->converter->convertFrom($messageBody, "subscribe to notifications");
            fail("MissingMandatoryParametersException was expected");
        } catch (MissingMandatoryParametersException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a case where
     * a mandatory parameter is missing for the "subscribe to notifications"
     * operation by throwing MissingMandatoryParametersException.
     * </p>
     *
     * <p>
     * MissingMandatoryParametersException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SubscribeToNotifications_MissingMandatoryParam4() {
        $messageBody = TestHelper::buildMessageBody(array(
            "destinationAddress", "notifyURL", "criteria", "callbackData", "clientCorrelator"),
            array("dstAddr", "http://notify2", "none", "callback2", "correlate"));
        try {
            $this->converter->convertFrom($messageBody, "subscribe to notifications");
            fail("MissingMandatoryParametersException was expected");
        } catch (MissingMandatoryParametersException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a case where
     * a mandatory parameter is missing for the "subscribe to notifications"
     * operation by throwing MissingMandatoryParametersException.
     * </p>
     *
     * <p>
     * MissingMandatoryParametersException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SubscribeToNotifications_MissingMandatoryParam5() {
        $messageBody = TestHelper::buildMessageBody(array(
            "destinationAddress", "notifyURL", "criteria", "notificationFormat", "clientCorrelator"),
            array("dstAddr", "http://notify2", "none", "plaintext", "correlate"));
        try {
            $this->converter->convertFrom($messageBody, "subscribe to notifications");
            fail("MissingMandatoryParametersException was expected");
        } catch (MissingMandatoryParametersException $e) {
            // success
        }
    }

    /**
     * <p>
     * Tests that the convertFrom(string, string) method handles a case where
     * a mandatory parameter is missing for the "subscribe to notifications"
     * operation by throwing MissingMandatoryParametersException.
     * </p>
     *
     * <p>
     * MissingMandatoryParametersException is expected.
     * </p>
     */
    public function testConvertFrom_Failure_SubscribeToNotifications_MissingMandatoryParam6() {
        $messageBody = TestHelper::buildMessageBody(array(
            "destinationAddress", "notifyURL", "criteria", "notificationFormat", "callbackData"),
            array("dstAddr", "http://notify2", "none", "plaintext", "callback2"));
        try {
            $this->converter->convertFrom($messageBody, "subscribe to notifications");
            fail("MissingMandatoryParametersException was expected");
        } catch (MissingMandatoryParametersException $e) {
            // success
        }
    }
}

?>